package za.ac.wits.was.controller;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import za.ac.wits.was.courses.Course;
import za.ac.wits.was.courses.CourseService;
import za.ac.wits.was.disciplines.Discipline;
import za.ac.wits.was.disciplines.DisciplineService;
import za.ac.wits.was.user.model.AcademicStaff;
import za.ac.wits.was.user.service.UserService;

/**
 *
 * @author Salman Noor : School of Electrical and Information Engineering
 * @Copyright: Wits University 2013 - All Rights Reserved
 * @email: salmannoor74415@gmail.com
 * @License: BSD License (http://www.linfo.org/bsdlicense.html)
 */
@Controller
@RequestMapping("/manage/*")
public class ManageController {

    Logger L = Logger.getLogger(ManageController.class);
    @Autowired
    private DisciplineService disciplineService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private UserService userService;
    
    @RequestMapping(value = "/Disciplines", method = RequestMethod.GET)
    public String disciplines(ModelMap model,
            final HttpServletRequest httpServletRequest, final HttpSession session) {
        httpServletRequest.setAttribute("activeLink", "Disciplines");
        return "managedisciplines";
    }

    @RequestMapping(value = "/Courses", method = RequestMethod.GET)
    public String courses(ModelMap model,
            final HttpServletRequest httpServletRequest, final HttpSession session) {
        httpServletRequest.setAttribute("activeLink", "Courses");
        return "managecourses";
    }
    
    @RequestMapping(value = "/StaffMembers", method = RequestMethod.GET)
    public String staffMembers(ModelMap model,
            final HttpServletRequest httpServletRequest, final HttpSession session) {
        httpServletRequest.setAttribute("activeLink", "StaffMembers");
        return "managestaffmembers";
    }

    @RequestMapping(value = "/disciplines/read", method = RequestMethod.POST)
    public @ResponseBody
    String readDisciplines() throws IOException {

        List<Discipline> ds = this.disciplineService.getAllDisciplines();
        L.debug("Discipline List is :" + ds.toString());
        return new ObjectMapper().writeValueAsString(ds);
    }

    @RequestMapping(value = "/disciplines/create", method = RequestMethod.POST)
    public @ResponseBody
    String createNewDiscipline(@RequestParam("models") String discipline) throws IOException {
        List<Discipline> target;
        ObjectMapper mapper = new ObjectMapper();
        target = mapper.readValue(discipline, new TypeReference<List<Discipline>>() {
        });
        L.debug("Updating discipline :" + target.toString());
        Discipline newDiscipline = this.disciplineService.createNewDiscipline(target.get(0));
        target.remove(0);
        target.add(newDiscipline);
        return new ObjectMapper().writeValueAsString(target);
    }

    @RequestMapping(value = "/disciplines/update", method = RequestMethod.POST)
    public @ResponseBody
    String updateDiscipline(@RequestParam("models") String discipline) throws IOException {
        List<Discipline> target;
        ObjectMapper mapper = new ObjectMapper();
        target = mapper.readValue(discipline, new TypeReference<List<Discipline>>() {
        });
        L.debug("Updating discipline :" + target.toString());
        this.disciplineService.updateDiscipline(target.get(0));
        return new ObjectMapper().writeValueAsString(target);
    }

    @RequestMapping(value = "/disciplines/delete", method = RequestMethod.POST)
    public @ResponseBody
    String deleteDiscipline(@RequestParam("models") String discipline) throws IOException {
        List<Discipline> target;
        ObjectMapper mapper = new ObjectMapper();
        target = mapper.readValue(discipline, new TypeReference<List<Discipline>>() {
        });
        L.debug("Deleting discipline :" + target.toString());
        this.disciplineService.deleteDiscipline(target.get(0));
        return new ObjectMapper().writeValueAsString(target);
    }
    
    
    @RequestMapping(value = "/courses/create", method = RequestMethod.POST)
    public @ResponseBody
    String createNewCourse(@RequestParam("models") String course) throws IOException {
        List<Course> target;
        ObjectMapper mapper = new ObjectMapper();
        target = mapper.readValue(course, new TypeReference<List<Course>>() {
        });
        L.debug("Creating Course :" + target.toString());
        Course newCourse = this.courseService.createNewCourse(target.get(0));
        target.remove(0);
        target.add(newCourse);
        return new ObjectMapper().writeValueAsString(target);
    }
    
    @RequestMapping(value = "/courses/read", method = RequestMethod.POST)
    public @ResponseBody
    String readCourses() throws IOException {

        List<Course> cl = this.courseService.getAllCourses();
        L.debug("Course List is :" + cl.toString());
        return new ObjectMapper().writeValueAsString(cl);
    }
    
    
        @RequestMapping(value = "/courses/update", method = RequestMethod.POST)
    public @ResponseBody
    String updateCourse(@RequestParam("models") String course) throws IOException {
        List<Course> target;
        ObjectMapper mapper = new ObjectMapper();
        target = mapper.readValue(course, new TypeReference<List<Course>>() {
        });
        L.debug("Updating course :" + target.toString());
        this.courseService.updateCourse(target.get(0));
        return new ObjectMapper().writeValueAsString(target);
    }
        
        
    @RequestMapping(value = "/courses/delete", method = RequestMethod.POST)
    public @ResponseBody
    String deleteCourse(@RequestParam("models") String course) throws IOException {
        List<Course> target;
        ObjectMapper mapper = new ObjectMapper();
        target = mapper.readValue(course, new TypeReference<List<Course>>() {
        });
        L.debug("Deleting course :" + target.toString());
        this.courseService.deleteCourse(target.get(0));
        return new ObjectMapper().writeValueAsString(target);
    }
    
    @RequestMapping(value = "/staffmembers/read", method = RequestMethod.POST)
    public @ResponseBody
    String readStaffMembers() throws IOException {

        List<AcademicStaff> al = this.userService.getAllAcademicStaff();
        L.debug("Academic Staff List is :" + al.toString());
        return new ObjectMapper().writeValueAsString(al);
    }
    
    
    @RequestMapping(value = "/staffmembers/create", method = RequestMethod.POST)
    public @ResponseBody
    String createNewStaffMember(@RequestParam("models") String staffMember) throws IOException {
        List<AcademicStaff> target;
        ObjectMapper mapper = new ObjectMapper();
        target = mapper.readValue(staffMember, new TypeReference<List<AcademicStaff>>() {
        });
        L.debug("Creating Staff Member :" + target.toString());
        AcademicStaff newStaffMember = this.userService.createNewAcademicStaff(target.get(0));
        target.remove(0);
        target.add(newStaffMember);
        return new ObjectMapper().writeValueAsString(target);
    }
    
    @RequestMapping(value = "/staffmembers/update", method = RequestMethod.POST)
    public @ResponseBody
    String updateStaffMember(@RequestParam("models") String staffMember) throws IOException {
        List<AcademicStaff> target;
        ObjectMapper mapper = new ObjectMapper();
        target = mapper.readValue(staffMember, new TypeReference<List<AcademicStaff>>() {
        });
        L.debug("Updating staff member :" + target.toString());
        this.userService.updateAcademicStaff(target.get(0));
        return new ObjectMapper().writeValueAsString(target);
    }
    
    
    @RequestMapping(value = "/staffmembers/delete", method = RequestMethod.POST)
    public @ResponseBody
    String deleteStaffMember(@RequestParam("models") String staffMember) throws IOException {
        List<AcademicStaff> target;
        ObjectMapper mapper = new ObjectMapper();
        target = mapper.readValue(staffMember, new TypeReference<List<AcademicStaff>>() {
        });
        L.debug("Deleting staff member :" + target.toString());
        this.userService.deleteAcademicStaff(target.get(0));
        return new ObjectMapper().writeValueAsString(target);
    }
    
    
    @RequestMapping(value = "/staffmembers/check/username", method = RequestMethod.POST)
    public @ResponseBody
    Boolean UsernameExists(@RequestParam("username") String username) throws IOException {
         L.debug("Checking user name exists :" + username);
        return this.userService.userExists(username);
    }
}
